#include <iostream>
#include <string>
#include <unistd.h>
#include <cstring>
#include <fcntl.h>

using namespace std;

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    string tips = "Please Enter# ";
    char buffer[1024];
    SetNonBlock(0);
    while (true)
    {
        write(0, tips.c_str(), tips.size());
        ssize_t n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "echo# " << buffer << endl;
        }
        else if (n == 0)
        {
            cout << "read file end!" << endl;
            break;
        }
        else
        {
            cout << "read error: " << n << " " << strerror(errno) << endl;
        }
        sleep(1);
    }
    return 0;
}